package com.f6.weixin.qiantai.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.f6.exceptions.BusinessException;
import com.f6.service.CommonService;
import com.f6.utils.SystemConstans;
import com.f6.vo.DBParameter;
import com.f6.weixin.common.vo.WeiXinOauth2Token;
import com.f6.weixin.utils.WeiXinUtils;
import com.f6.weixin.utils.WeixinAuthUtil;

@Controller
@RequestMapping(SystemConstans.QT)
public class SellController {

	private Logger logger = LoggerFactory.getLogger(SellController.class);

	@Autowired
	private CommonService commonservice;

	@RequestMapping(value = "prodlist", method = RequestMethod.GET, produces = "application/text;charset=UTF-8")
	public ModelAndView dishlist(HttpServletRequest request) throws BusinessException {
		logger.info("enter prodlist");
		String openid = getOpenID(request);

		// String currentUserOpenID =
		// flashattrs.get(SystemConstans.CURRENT_USER);
		logger.info("========currentUserOpenID=========" + openid);

		ModelAndView mv = new ModelAndView();
		mv.setViewName("qt/dishlist1");
		mv.addObject("openid", openid);
		Map paramap = new HashMap<String, String>();
		DBParameter dbparam = new DBParameter();
		dbparam.setAction("selectDishList");
		dbparam.setModule("putong.Dish");

		dbparam.setParameter(paramap);

		// List<Map<String, ?>> list = commonservice.queryMore(dbparam);
		// List<Dish> list = new ArrayList<Dish>();
		// logger.info("获取列表:" + list.size());
		// mv.addObject("dishlist", list);
		return mv;
	}

	private String getOpenID(HttpServletRequest request) {
		String openid = null;
		String code = request.getParameter("code");
		logger.info("========current code=========" + code);
		if (code != null) {
			WeiXinOauth2Token weiXinOauth2Token = WeixinAuthUtil.getOauth2AccessToken(SystemConstans.APPID, SystemConstans.APPSECRET, code);
			openid = weiXinOauth2Token.getOpenId();
		}
		return openid;
	}

	@RequestMapping(value = "myprofile", method = RequestMethod.GET, produces = "application/text;charset=UTF-8")
	public ModelAndView myprofile() throws BusinessException {
		logger.info("enter myprofile");
		ModelAndView mv = new ModelAndView();
		// String accesstoken = WeiXinUtils.getAccessToken(appid, appsecret);
		// WeiXinUtils.getUserDetail(accesstoken, openid);
		mv.setViewName("qt/myprofile");

		return mv;
	}

	@RequestMapping(value = "qrcode1", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String generateQRCode(HttpServletRequest request) {
		String url = WeiXinUtils.createOAuthURI(SystemConstans.WEIXIN_AUTH_REIDRECT_URL_QRCODE);
		return url;
	}

	@RequestMapping(value = "proddetail", method = RequestMethod.GET, produces = "application/text;charset=UTF-8")
	public ModelAndView proddetail() throws BusinessException {
		logger.info("enter dishlist");
		ModelAndView mv = new ModelAndView();
		mv.setViewName("qt/dishdetail");

		Map paramap = new HashMap<String, String>();
		DBParameter dbparam = new DBParameter();
		dbparam.setAction("selectDishList");
		dbparam.setModule("putong.Dish");
		dbparam.setParameter(paramap);

		List<Map<String, ?>> list = commonservice.queryMore(dbparam);
		// List<Dish> list = new ArrayList<Dish>();
		logger.info("获取列表:" + list.size());
		mv.addObject("dishlist", list);
		return mv;
	}

}
